package com.wuhongyu.rabbitmq.producer.rabbitmq;

import com.wuhongyu.api.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@Slf4j
public class Producer {


    private RabbitTemplate rabbitTemplate;

    @Autowired
    private void setRabbitTemplate(RabbitTemplate rabbitTemplate){
        rabbitTemplate.setConfirmCallback(((correlationData, b, s) -> {
            if (!b){
                log.error("correlationData: {}",correlationData);
                log.error("b: {}",b);
                log.error("s: {}",s);
                log.error("未收到消息确认： {}",correlationData);
                log.error("原因是： {}",s);
            }
        }));
        rabbitTemplate.setReturnCallback((var1,var2,var3, var4, var5)->{
            log.error("var1: {}",var1);
            log.error("var2: {}",var2);
            log.error("var3: {}",var3);
            log.error("var4: {}",var4);
            log.error("var5: {}",var5);
        });
        this.rabbitTemplate = rabbitTemplate;
    }





    public void send(Message message){
        String id = UUID.randomUUID().toString();
        message.setId(id);
        CorrelationData correlationData = new CorrelationData(id);
        log.info("生产者发送消息： {}",message);
        log.info("消息id为： {}",id);
        rabbitTemplate.convertAndSend(RabbitmqConstant.EXCHANGE,RabbitmqConstant.ROUTEING_KEY,message,correlationData);

    }
}
